/** 
 * @(#)NetworkDemoRIP.java
 */

/**
 * @Didier Gutacker
 * @version 1.0
 * @see Demo 
 * @see NetworkRIP
 * @see LinkRIP
 * @see NodeRIP
 *
 */

public class NetworkDemoRIP extends Demo {
  NetworkRIP net;

  public NetworkDemoRIP( NetworkRIP net ) {
    this.net = net;
    this.running = false;
    t = new Thread( net.getThreadGroup(), this );
    t.setPriority( Thread.MIN_PRIORITY );
    infoWindow = new InfoWindow( new String( "RIP Routing Demo" ), 20, 70 );
  } // NetworkDemoRIP.NetworkDemoRIP

  public void run() {
    infoWindow.expose( net.getSimulation().getText().demoGreetings );
    infoWindow.waitForConfirmation();
    NodeRIP nodeA = net.addNode( 10, 10 );
    NodeRIP nodeB = net.addNode( 10, 250 );
    LinkRIP linkAB = net.addLink( nodeA, nodeB );
    delay( 2 );
    infoWindow.expose( net.getSimulation().getText().demoPart1 );
    infoWindow.waitForConfirmation(); 
    NodeRIP nodeC = net.addNode( 300, 10 );
    NodeRIP nodeD = net.addNode( 200, 250 );
    NodeRIP nodeE = net.addNode( 360, 250 );
    LinkRIP linkAC = net.addLink( nodeA, nodeC );
    LinkRIP linkCD = net.addLink( nodeC, nodeD );
    LinkRIP linkCE = net.addLink( nodeC, nodeE );
    delay( 3 );
    net.startSending( nodeA, nodeD );
    delay( 2 );
    infoWindow.expose( net.getSimulation().getText().demoPart2 );
    infoWindow.waitForConfirmation();
    net.stopSending( nodeA );
    delay( 1 );
    net.inspectRoutingTable( nodeC );
    delay( 1 );
    NodeRIP nodeF = net.addNode(580,250);
    LinkRIP linkEF = net.addLink( nodeE, nodeF );
    delay( 4 );
    LinkRIP linkCF = net.addLink( nodeC, nodeF );
    delay( 6 );
    infoWindow.expose( net.getSimulation().getText().demoPart3 );
    infoWindow.waitForConfirmation();
    delay( 1 );
    net.startSending( nodeA, nodeF );
    delay( 4 );
    infoWindow.expose( net.getSimulation().getText().demoPart4 );
    infoWindow.waitForConfirmation();
    net.stopSending( nodeA );
    net.inspectRoutingTable( nodeA );
    delay( 1 );
    net.removeLink( linkCF );
    delay( 7 );
    infoWindow.expose( net.getSimulation().getText().demoPart5 );
    infoWindow.waitForConfirmation();
    delay( 1 );
    net.startSending( nodeA, nodeF );
    delay( 4 );
    infoWindow.expose( net.getSimulation().getText().demoPart6 );
    infoWindow.waitForConfirmation();
    net.stopSending( nodeA);
    delay( 1 );
    net.inspectRoutingTable( nodeA );
    delay( 1 );
    linkCF = net.addLink( nodeC, nodeF ); 
    delay( 8 );
    net.startSending( nodeA, nodeF );
    delay( 4 );
    infoWindow.expose( net.getSimulation().getText().demoEnd );
    infoWindow.waitForConfirmation();
    running = false;
    net.stopDemo();
  } // NetworkDemoRIP.run

  final void delay() {
    int speed = net.getSpeed();
    int duration;
    if ( speed == 0 ) {
      duration = 10000;
    } else if ( speed == 1 ) {
      duration = 6000;
    } else {
      duration = 3000;
    } // if
    try {
      Thread.sleep( duration );
    } catch( InterruptedException e ) {
    } // try
  } // NetworkDemoRIP.delay

  final void delay( int times ) {
    for ( int i = 0; i < times; i += 1 ) {
      delay();
    } // for
  } // NetworkDemoRIP.delay

} // NetworkDemoRIP
